$(function () {
    let layer = layui.layer
    let form = layui.form

    //获取链接列表
    function getLink() {
        $("#tb").empty()
        $.ajax({
            url: "admin/links",
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                $(template("template", res)).appendTo($("#tb"))
            }

        })
    }
    getLink()

    //添加链接
    let index
    $(".addBtn").click(function () {
        index = layer.open({
            title: '添加友情链接',
            type: 1,
            area: ['500px', '350px'],
            content: $("#linkTemp").html() //这里content是一个普通的String 500 350
        });
    })

    //监听文件选中事件
    $("body").on("change","#upload",function(event){
        let objectURL = URL.createObjectURL(event.target.files[0])
        console.log(objectURL)
        $("#imgWrap").show().attr("src",objectURL)
    })

    //美化文件上传按钮
    $("body").on("click", ".udpBtn", function () {
        $("#upload").click()
    })

    //添加链接的表单提交事件admin/links
    $("body").on("submit", "#addLink", function (event) {
        event.preventDefault()
        // console.log(1)
        let fd = new FormData(this)
        // console.log(event)
        let files = event.target[3].files[0]
        let send = "post"
        //判断是post请求还是get请求
        if(sign){
            send = "put"
        }else{
            fd.append("linkicon", files)
        }
        
        // console.log(files)

        $.ajax({
            url: "admin/links/"+sign,
            type: send,
            data: fd,
            contentType: false,
            processData: false,
            success: function (res) {
                // console.log(res)
                if (res.status !== 0) {
                    return
                }
                $("#imgWrap").hide()
                sign=""
                layer.close(index)
                layer.msg(res.message)
                getLink()
            }
        })

    })

    let sign = "" //此变量用于区分是编辑请求还是添加请求
    //编辑按钮事件
    $("#tb").on("click",".editBtn",function(){
        // console.log(1)
        $(".addBtn").click()

        let id = $(this).attr("data-id")
        // console.log(id)
        sign=id
        $.ajax({
            url:"admin/links/"+id,
            success: function(res){
                // console.log(res)
                if(res.status !== 0){
                    return 
                }
                layer.msg(res.message)

                form.val("friendLink",res.data)
                $("#imgWrap").attr("src",res.data.linkicon)
                console.log(res.data.linkicon)
            }

        })
    })

    //删除链接按钮
    $("#tb").on("click", ".delBtn", function () {
        // console.log(1)
        let id = $(this).attr("data-id")
        // console.log(id)
        layer.confirm('确认要删除吗?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                url: "admin/links/" + id,
                type: "delete",
                success: function (res) {
                    // console.log(res)
                    if (res.status !== 0) {
                        return layer.msg(res.message)
                    }
                    layer.msg(res.message)
                    //删除成功在次获取一下
                    getLink()
                }
            })

            layer.close(index);
        });
    })
})